package com.example.cdctest.managecontroller;

import com.example.cdctest.pojo.Admin;
import com.example.cdctest.pojo.Answers;
import com.example.cdctest.pojo.Config;
import com.example.cdctest.pojo.Question;
import com.example.cdctest.service.CategoryService;
import com.example.cdctest.service.QuestionService;
import com.example.cdctest.service.QuestionTypeService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
@RequestMapping("/manage/question")
public class QuestionController
{
    @Resource
    private QuestionService questionService;

    @Resource
    private QuestionTypeService questionTypeService;

    @Resource
    private CategoryService categoryService;


    //跳转到添加问题页面
    @RequestMapping("question-add")
    public ModelAndView questionAdd(){
        ModelAndView modelAndView=new ModelAndView();
        modelAndView.addObject("categorys",categoryService.findAll());
        modelAndView.addObject("questionTypeList",questionTypeService.findAll());
        modelAndView.setViewName("/manage/question/question-add");
        return modelAndView;
    }

    //添加问题,问题类别，问题类型，问题题目，问题正确选项，问题错误选项，添加管理员id，问题解析
    @RequestMapping("add-question")
    @ResponseBody
    public void addQuestion(String subject, String explain, String answer, String trueAnswer, Integer questionTypeId, Integer categoryId, HttpServletRequest request){
        String[] answers=answer.split(",");
        String[] trueAnswers=trueAnswer.split(",");
       Admin loginAdmin=(Admin)request.getSession().getAttribute("loginAdmin");
        questionService.insertQuestion(subject,explain,answers,trueAnswers,questionTypeId,categoryId,loginAdmin);
    }

    //跳转到问题管理的页面,包括问题的答案选项
    @RequestMapping("question-list")
    public ModelAndView questionList(){
        ModelAndView modelAndView=new ModelAndView();
        modelAndView.addObject("questionTypeList",questionTypeService.findAll());
        modelAndView.setViewName("/manage/question/question-list");
        return modelAndView;
    }
    //通过查询的条件，查询相关的问题
    @RequestMapping("question-list-page")
    @ResponseBody
    public ModelAndView questionListPage(String subject,Integer pageNum,HttpServletRequest request,Integer questionTypeId){
        ModelAndView modelAndView=new ModelAndView();
        if(pageNum==null) pageNum=1;
        Integer pageSize=10;
        ServletContext servletContext=request.getSession().getServletContext();
        List<Config> configList=(List<Config>)servletContext.getAttribute("configs");
        for (Config config:configList
             ) {
            if("manage_question_count".equals(config.getKey())){
                pageSize=Integer.parseInt(config.getValue());
            }
        }
        if(questionTypeId==null) questionTypeId=0;
        //查出相关条件的问题的数量
        modelAndView.addObject("questionCount",questionService.getQuestionCount(subject,pageNum,pageSize,questionTypeId));
        modelAndView.addObject("pageInfo",questionService.findAllQuestions(subject,pageNum,pageSize,questionTypeId));
        modelAndView.addObject("subject",subject);
        modelAndView.setViewName("/manage/question/question-data-list");
        return modelAndView;
    }

    //转发到修改问题选项的页面
    @RequestMapping("answer-update")
    public ModelAndView answerUpdate(Integer id,String subject)
    {
        ModelAndView modelAndView=new ModelAndView();
        modelAndView.addObject("subject",subject);
        modelAndView.addObject("answers",questionService.getAnswersById(id));
        modelAndView.setViewName("/manage/question/answer-update");
        return modelAndView;
    }

    //通过当前问题的id,查出这个问题的标题,问题的解析
    @RequestMapping("question-update")
    public ModelAndView questionUpdate(Integer id,String subject,String explain){
        ModelAndView modelAndView=new ModelAndView();
        modelAndView.addObject("id",id);
        modelAndView.addObject("subject",subject);
        modelAndView.addObject("explain",explain);
        modelAndView.setViewName("/manage/question/question-update");
        return modelAndView;
    }

    //通过问题选项的id，修改问题的选项内容
    @RequestMapping("update-answer")
    @ResponseBody
    public void updateAnswer(Answers answer){
         questionService.updateAnswer(answer);
    }

    //通过问题的id,重新编辑的问题标题,重新编辑好的问题解析,提交到数据库修改信息参数
    @RequestMapping("update-question")
    public String updateQuestion(Question question) {
         questionService.update(question);
         return "/manage/index";
    }

    //通过问题的id，删除该id的问题,以及该问题下所有的选项
    @RequestMapping("deleteQuestion")
    @ResponseBody
    public void deleteQuestion(Integer id){
          questionService.updateQuestionById(id);
    }

    //通过批量问题的id，删除这些id下的问题，及其对应的选项
    @RequestMapping("batch-question")
    @ResponseBody
    public void batchQuestion(String ids){
        questionService.updateBatchQuestion(ids);
    }
}
